//
//  MyTabBarController.swift
//  WangPu
//
//  Created by xiaozhi on 15/9/15.
//  Copyright (c) 2015年 zhixiao. All rights reserved.
//

import UIKit

class MyTabBarController: UITabBarController,UINavigationControllerDelegate{
    var myBar:MyTabBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.hidden = true
          myBar = MyTabBar(frame: CGRectMake(0, view.bounds.size.height-tabbarHeight, windowWidth, tabbarHeight))
            myBar.buttonEventHandle = {buttonTag in
            let selectNumber = (buttonTag > 2) ? (buttonTag-1) : buttonTag
            self.selectedIndex = selectNumber
        }
        view.addSubview(myBar)
        // Do any additional setup after loading the view.
    }
//    func hiddenWhenPushed(){
//        myBar.hidden = true;
//    }
//    func showBar(){
//        myBar.hidden = false;
//    }
    func setSelectCtl(index:NSInteger){
        myBar.selectIndex(index)
//        self.selectedIndex = index
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        if let controllers = self.viewControllers{
            for ctl in controllers{
                if let nav = ctl as? UINavigationController{
                    nav.delegate = self
                }
            }
        }

    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //        beforePriceLabel.addColorWithreallySize()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
extension MyTabBarController{
    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool){
        let root: UIViewController = navigationController.viewControllers[0] 
        if viewController != root{
            navigationController.view.frame = self.view.bounds
            myBar.removeFromSuperview()
            var rect = myBar.frame
            if root.view  is UIScrollView{
                
            }
            else{
               rect.origin.y = root.view.bounds.size.height - tabbarHeight
            }
            myBar.frame = rect
            root.view.addSubview(myBar)
        }
        tabBar.hidden = true
    }
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        let root: UIViewController = navigationController.viewControllers[0] 
        if viewController == root{
            navigationController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - tabbarHeight)
            myBar.removeFromSuperview()
            myBar.frame = CGRectMake(0, self.view.bounds.size.height - tabbarHeight, self.view.bounds.size.width, tabbarHeight)
            view.addSubview(myBar)
        }
        tabBar.hidden = true

    }
}